﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using YDModel;
using YDWin.Base;
using YDCommon;
using System.Globalization;

namespace YDWin
{
    public partial class CDKForm : AddForm
    {
        public CDKForm()
        {
            InitializeComponent();
        }

        private void btnVerification_Click(object sender, EventArgs e)
        {
            try
            { 
                string cdk = txtCDK.Text;
                if (!string.IsNullOrEmpty(cdk))
                {
                    byte[] bytIn = Convert.FromBase64String(cdk);
                    var newcdk = ASCIIEncoding.ASCII.GetString(bytIn);
                    newcdk = newcdk.ToLower();
                    string part1 = newcdk.Substring(41, 5);
                    string part2 = newcdk.Substring(11, 5);
                    string part3 = newcdk.Substring(25, 4);
                    string part = part1 + part2 + part3;  

                    if (part.Equals("00ab66cd4245rt"))//无期限
                    {
                        Utils.ShowPrompt(MessageType.mtInformation, "恭喜您开通无期限版本");
                        //MessageBox.Show("恭喜您开通无期限版本");
                    }
                    else
                    {
                        //检测CDK中的日期是否有效
                        DateTime newPart = Convert.ToDateTime(part.Substring(0, 4) + "-" + part.Substring(4, 2) + "-" + part.Substring(6, 2));
                         
                        var date = part.Substring(0, 4) + '年' + part.Substring(4, 2) + '月' + part.Substring(6, 2) + "日";
                        //MessageBox.Show("您注册的版本到期日期为:" + date);
                        Utils.ShowPrompt(MessageType.mtInformation, "您注册的版本到期日期为:" + date);
                    }

                    //保存新的CDK
                    using (YDDataEntities ye = new YDDataEntities())
                    {
                        var sp = ye.T_System_Parameter.Where(o => o.Parameter_Name == "CDK").FirstOrDefault();
                        if (sp == null)
                        {
                            T_System_Parameter tsp = new T_System_Parameter();
                            tsp.Parameter_Name = "CDK";
                            tsp.Parameter_Value = cdk;
                            tsp.Operatint_Time = DateTime.Now;
                            ye.T_System_Parameter.Add(tsp);
                        }
                        else
                        {
                            sp.Parameter_Value = cdk;
                        }

                        ye.SaveChanges();
                    }

                }
            }catch (Exception)
            {
                Utils.ShowPrompt(MessageType.mtWarning, "无效的CDK");
                //MessageBox.Show("无效的CDK");
            }
        }

        private void CDKForm_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
        }

        private void CDKForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            //按esc关闭界面
            if (e.KeyChar == (char)Keys.Escape)
            {
                this.Close();
            }
        }
    }
}